package dr.incomeapp.dao;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.j256.ormlite.dao.Dao;

import dr.incomeapp.Application;
import dr.incomeapp.bean.Bean;

public abstract class GenericDao<T extends Bean, K> {

	protected Dao<T, Integer> dao = null;

	public GenericDao() {
		dao = getDao();
	}

	public List<T> readAll() {
		List<T> list = new LinkedList<T>();
		try {
			
			list = dao.queryForAll();
		} catch (SQLException e) {

			Application.log(this.getClass().getName(), e);
		}
		return list;
	}

	public T get(int id) {

		T bean = null;
		try {
			bean = dao.queryForId(id);
		} catch (SQLException e) {

			Application.log(this.getClass().getName(), e);
		}
		return bean;

	}
	
	public void delete(T bean){
		try{
			dao.delete(bean);
		}catch (SQLException e) {

			Application.log(this.getClass().getName(), e);
		}
	}

	public void createOrUpdate(T bean) {

		try {
		
			dao.createOrUpdate(bean);
		} catch (SQLException e) {

			Application.log(this.getClass().getName(), e);
		}
	}

	public Dao<T, Integer> getDao() {
		Dao<T, Integer> dao = null;
		try {
			dao = Application.GetInstance().getDataBaseHelper()
					.getDao(getBeanClass());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dao;
	}

	public abstract Class<T> getBeanClass();

}
